// login.js
var util = require('../../utils/util.js');
var validate = require('../../utils/validate.js');
var cart = require('../../utils/cart.js');
var commonapi = require('../../component/service/commonapi.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    returnType:false,
    mobile: "",
    password: "",
    imageCaptcha: "",
    smsCaptcha:"",
    authButton:false,
    channel:'',//跳转渠道 shopping为购物清单
    verifyCode:"发送验证码",
    tokenKey:'',
    imgCaptcha:'',
    imgToken:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let returnType = false;
    let tokenKey = '';
    if(typeof(options.returnType)!="undefined"&&(options.returnType)){
      returnType = true;
    }
    if(typeof(options.tokenKey)!="undefined"){
      tokenKey = options.tokenKey;
    }
    this.setData({
      returnType:returnType,
      tokenKey:tokenKey
    })
    this.changeImgCaptcha();
  },
  //切换到登录页面时，尝试登录一次
  onShow:function(){
    commonapi.login(function(){
      if(getApp().globalData.platUser!=null){
        wx.navigateBack();
      }      
    })
  },
  //获取图形验证码
  changeImgCaptcha:function(){
    let imgToken = util.uuid();
    this.setData({
      imgCaptcha:getApp().globalData.host + 'webapp/control/captcha-img.html?imgToken=' + imgToken,
      imgToken:imgToken
    })
  },
  inputMobile: function(e){
    this.setData({
      mobile: e.detail.value
    })
  },

  inputPswd: function(e){
    this.setData({
      password: e.detail.value
    })
  },

  inputCaptcha: function(e){
    this.setData({
      imageCaptcha: e.detail.value
    })
  },
  inputSmsCaptcha: function (e) {
    this.setData({
      smsCaptcha: e.detail.value
    })
  },
  sendSMS: function (e) {
    if (!validate.validateMobile(this.data.mobile))
      return;
    util.auth_mobile(this, this.data.mobile);
  },
  register:function(){
    wx.navigateTo({
      url: '../register/register?returnType=' + this.data.returnType + '&tokenKey=' + this.data.tokenKey
    });
  },
  toIndex:function(){
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //获取用户手机号 并注册新用户
  getPhoneNumber: function(e) {
    if(commonapi.verifySDKVersion(1.2)&&e.detail.errMsg == 'getPhoneNumber:ok'){
      commonapi.onekeyRegister(e.detail.iv,e.detail.encryptedData,this.data.returnType,1,this.data.tokenKey);
    }
  },

  login: function(e){
    var mobile = this.data.mobile;
    var password = this.data.password;
    var smsCaptcha = this.data.smsCaptcha;
    let imageCaptcha = this.data.imageCaptcha;

    if (!validate.validateMobile(mobile))
      return;
   
    /** 校验密码规则待确认
    if (!validate.validatePass(password))
      return;
    */
   
    if(!validate.validateEmpty(imageCaptcha,'图形验证码不得为空'))
      return;
    if(password.length == 0){
      wx.showModal({
        title:'请输入密码',
        showCancel:false
      })
      return;
    }
    /** 不使用短信验证码
    if (smsCaptcha.length == 0) {
      wx.showToast({
        title: '请输入验证码',
      })
      return;
    }
    */
    let that = this;
    //获取loginCode
    wx.login({
      success: function (resp) {
        wx.request({
          data: { mobile: mobile, password: password, smsCaptcha: smsCaptcha, imgCaptcha:imageCaptcha,wechatCode: resp.code,imgToken: that.data.imgToken},
          url: getApp().globalData.host + "/webapp/control/bindWechat.html",
          header: getApp().globalData.header,
          success: function (res) {
            if (res.data.success) {
              if (res.data.success) {
                getApp().globalData.header.cookie = "YNITTC_SOURCE=WECHAT;YNITTC_SESSION_ID=" + res.data.token;
                getApp().globalData.platUser = res.data.user;
                /**
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2]; //上一个页面
                wx.switchTab({
                  url: '../../pages/userCenter/userCenter'
                })
                */
                cart.register();//将用户本地购物清单添加至server

                //返回上一页无效，只能返回首页
                wx.navigateBack();
              }
            } else {
              wx.showModal({
                title:  '绑定失败',
                content:res.data.errorMsg,
                showCancel:false
              })
            }
          }
        })
      }
    })
  }
  
})
